#include "winmgr.h"

#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include "bgfx/bgfx.h"
#include "bgfx/platform.h"

#include "bgfx_imgui.h"

#include <bx/thread.h>
#include <bx/os.h>
#include <bx/handlealloc.h>
#include <bx/mutex.h>
#include "imgui_impl_sdl.h"
#include "bgfx_imgui.h"
winmgr::winmgr()
{
}
winmgr *winmgr::g_this = nullptr;

winmgr *winmgr::Instance()
{
    if (winmgr::g_this == nullptr)
        winmgr::g_this = new winmgr();
    return winmgr::g_this;
}
static void OnGUI()
{
   if(ImGui::Begin("hello"))
   {
     ImGui::Text("ddd");
   }
    ImGui::End();
   
    ImGui::Text("hello");
}
void winmgr::MainLoop()
{
    SDL_Init(0);

    SDL_Window *winMain = SDL_CreateWindow("helloha", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

    //SDL_Window *win2 = SDL_CreateWindow("small", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 300, 200, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
    auto winid = SDL_GetWindowID(winMain);
    //auto winid2 = SDL_GetWindowID(win2);
    SDL_SysWMinfo wmi;
    //SDL_SysWMinfo wmi2;
    SDL_VERSION(&wmi.version);
    //SDL_VERSION(&wmi2.version);
    if (!SDL_GetWindowWMInfo(winMain, &wmi))
    {
        return;
    }
    // if (!SDL_GetWindowWMInfo(win2, &wmi2))
    // {
    //     return;
    // }
    bgfx::PlatformData pdata;
    if (wmi.subsystem == SDL_SYSWM_X11)
    {
        pdata.ndt = wmi.info.x11.display;
        pdata.nwh = (void *)wmi.info.x11.window;
    }
    else
    {
        throw "only support x11 fornow.";
    }
    //this->hwnd = (HWND)hwnd;
    //bgfx SetPlatformData ,��Ҫ�ǰ� hwnd (��windows��˵)

    // pdata.nwh = sdlNativeWindowHandle(win); //bind windows info
    bgfx::setPlatformData(pdata);

    bgfx::Init initopt;

    initopt.type = bgfx::RendererType::Enum::Count;
    initopt.resolution.reset = BGFX_RESET_VSYNC;
    int viewwidth = 0;
    int viewheight = 0;
    SDL_GetWindowSize(winMain, &viewwidth, &viewheight);
    initopt.resolution.width = viewwidth;
    initopt.resolution.height = viewheight;
    auto b = bgfx::init(initopt);

    bgfx::setDebug(BGFX_DEBUG_STATS);

    //init first

    auto caps = bgfx::getCaps();
    //TRACE("== bgfx::init = %s \n", b ? "true" : "false");

    //bgfx_imgui::init 在前
    bgfx_imgui::Init(nullptr, viewwidth, viewheight);
    ImGui_ImplSDL2_Init(winMain);
    //set clearcolor

    // auto fb = bgfx::createFrameBuffer((void *)wmi2.info.x11.window, 300, 200, bgfx::TextureFormat::RGBA8, bgfx::TextureFormat::D16);
    //bgfx::reset(viewwidth, viewheight);
    SDL_Event event;
    bool bexit = false;

    //最后的输出需要一锅烩
    //sdl有点闪烁，不管了，随他去吧。
    //编辑器闪烁有可能和bgfx的多线程有关
    //RT 可以单独一帧画，
    while (!bexit)
    {
        int w, h;
        SDL_GetWindowSize(winMain, &w, &h);
        if (w != viewwidth || h != viewheight)
        {
            bgfx::reset(w, h, BGFX_RESET_VSYNC);
            viewwidth = w;
            viewheight = h;
        }
        ImGui_ImplSDL2_NewFrame(winMain);
        ImGui::NewFrame();
        OnGUI();

        ImGui::Render();

        // //draw mainwindow
        //Main Render
        bgfx::setViewFrameBuffer(0, BGFX_INVALID_HANDLE);
        bgfx::setViewMode(0, bgfx::ViewMode::Sequential);
        bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0xFF33ddFF);
        bgfx::setViewRect(0, 0, 0, viewwidth, viewheight);
        bgfx::setViewScissor(0);

        bgfx::touch(0);

        bgfx_imgui::Render(0, ImGui::GetDrawData());
        // //bgfx::frame();

        // //draw smallwindow
        ImGui::UpdatePlatformWindows();
        ImGui::RenderPlatformWindowsDefault();
        // bgfx::setViewFrameBuffer(1, fb);
        // bgfx::setViewClear(1, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0xFF0033FF);
        // bgfx::setViewRect(1, 0, 0, 300, 200);
        // bgfx::touch(1);
        bgfx::frame();

        int b = SDL_PollEvent(&event);
        if(b)
        {
            ImGui_ImplSDL2_ProcessEvent(&event);
            switch (event.type)
            {

            case SDL_QUIT:
                //m_eventQueue.postExitEvent();
                bexit = true;
                break;
            case SDL_WINDOWEVENT:
            {

                if (event.window.event == SDL_WINDOWEVENT_RESIZED ||
                    event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
                    event.window.event == SDL_WINDOWEVENT_MAXIMIZED ||
                    event.window.event == SDL_WINDOWEVENT_RESTORED)
                {
                    if (event.window.windowID == winid)
                    {
                        // int w, h;
                        // SDL_GetWindowSize(winMain, &w, &h);
                        // bgfx::reset(w, h,BGFX_RESET_VSYNC);
                        // viewwidth=w;
                        // viewheight =h;
                    }
                }
                if (event.window.event == SDL_WINDOWEVENT_CLOSE)
                {
                    if (event.window.windowID == winid)
                    {
                        SDL_DestroyWindow(winMain);
                        bexit = true;
                    }
                    // else
                    // {
                    //     SDL_DestroyWindow(win2);
                    // }
                }
            }
            break;
            }
        }
    }
    //exit
    //here
}
void winmgr::CreateMainWin(const char *title, int w, int h, int nCmdShow, std::shared_ptr<IWin> winprocess)
{
}
void winmgr::CreateWin(const char *title, int w, int h, std::shared_ptr<IWin> winprocess)
{
}
void winmgr::ResetSubWin()
{
}

//protected:
int winmgr::GetFreeID()
{
}
void winmgr::OnExitWin(void *hwnd)
{
}